import path from "path";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
const pathSrc = path.resolve(__dirname, "src");
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";

// https://vitejs.dev/config/
export default defineConfig({
    resolve: {
        alias: {
            "~/": `${pathSrc}/`,
            "@": path.resolve(__dirname, "./src"),
            vue: path.resolve("./node_modules/vue"),
        },
    },
    plugins: [
        vue(),
        // @ts-ignore
        createSvgIconsPlugin({
            // 指定需要缓存的图标文件夹
            iconDirs: [path.resolve(process.cwd(), "src/assets/svg")],
            // 指定symbolId格式
            symbolId: "[name]",
        }),
    ],
    server: {
        proxy: {
            "/mock": {
                target: "http://www.yapi.sribd.com/mock/251", // 接口地址
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/mock/, "api"),
            },
            "/api": {
                target: "http://10.26.8.11:8087/", // 接口地址
                changeOrigin: true,
                // rewrite: (path) => path.replace(/^\/mock/, ""),
            },
        },
    },
    build: {
        // 压缩
        minify: "esbuild",
        sourcemap: true,
        // 进行压缩计算
        // @ts-ignore
        brotliSize: false,
        rollupOptions: {
            output: {
                assetFileNames: (assetInfo: any) => {
                    var info = assetInfo.name.split(".");
                    var extType = info[info.length - 1];
                    if (/\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/i.test(assetInfo.name)) {
                        extType = "media";
                    } else if (/\.(png|jpe?g|gif|svg)(\?.*)?$/.test(assetInfo.name)) {
                        extType = "img";
                    } else if (/\.(woff2?|eot|ttf|otf)(\?.*)?$/i.test(assetInfo.name)) {
                        extType = "fonts";
                    }
                    return `static/${extType}/[name]-[hash][extname]`;
                },
                // manualChunks: (id) => {
                //     if (idincludes('node_modules')) {
                //         return 'vandor'
                //     }
                // }
                chunkFileNames: "static/js/[name]-[hash].js",
                entryFileNames: "static/js/[name]-[hash].js",
            },
        },
        terserOptions: {
            compress: {
                drop_console: true,
                drop_debugger: true,
            },
        },
    },
});
